﻿using System;
namespace GBlog.Core.Recaptcha
{
    public class RecaptchaResponse
    {
        public static readonly RecaptchaResponse Valid = new RecaptchaResponse(true, string.Empty);
        public static readonly RecaptchaResponse InvalidChallenge = new RecaptchaResponse(false, "Invalid reCAPTCHA request. Missing challenge value.");
        public static readonly RecaptchaResponse InvalidResponse = new RecaptchaResponse(false, "Invalid reCAPTCHA request. Missing response value.");
        public static readonly RecaptchaResponse InvalidSolution = new RecaptchaResponse(false, "The verification words are incorrect.");
        public static readonly RecaptchaResponse RecaptchaNotReachable = new RecaptchaResponse(false, "The reCAPTCHA server is unavailable.");
        private bool isValid;
        private string errorMessage;
        public bool IsValid
        {
            get
            {
                return this.isValid;
            }
        }
        public string ErrorMessage
        {
            get
            {
                return this.errorMessage;
            }
        }
        internal RecaptchaResponse(bool isValid, string errorMessage)
        {
            RecaptchaResponse recaptchaResponse = null;
            if (this.IsValid)
            {
                recaptchaResponse = RecaptchaResponse.Valid;
            }
            else
            {
                if (errorMessage == null)
                {
                    throw new ArgumentNullException("errorMessage");
                }
                if (errorMessage == "incorrect-captcha-sol")
                {
                    recaptchaResponse = RecaptchaResponse.InvalidSolution;
                }
            }
            if (recaptchaResponse != null)
            {
                this.isValid = recaptchaResponse.IsValid;
                this.errorMessage = recaptchaResponse.ErrorMessage;
            }
            else
            {
                this.isValid = isValid;
                this.errorMessage = errorMessage;
            }
        }
        public override bool Equals(object obj)
        {
            RecaptchaResponse recaptchaResponse = (RecaptchaResponse)obj;
            return recaptchaResponse != null && recaptchaResponse.IsValid == this.isValid && recaptchaResponse.ErrorMessage == this.errorMessage;
        }
        public override int GetHashCode()
        {
            return this.isValid.GetHashCode() ^ this.errorMessage.GetHashCode();
        }
    }
}